import Vue from 'vue'
// import {vm} from '@/main.js'
import Router from 'vue-router'
import App from '@/App'
import searchIndex from '@/components/search/index'
import mineIndex from '@/components/mine/index'
import myCard from '@/components/mine/index/myCard'
import addCard from '@/components/mine/index/addCard'

import {wexinsdk} from '../js/wxsdk.js'
import {store} from '../main.js'
import {hasuid} from '../js/islogin.js'
import {isWeiXin,towxurl,tovueurl,getCookie,setCookie,GetQueryString} from '../config/common.js'

const reply = r => require.ensure([], () => r(require('@/components/common/reply.vue')), 'reply')
const blog = r => require.ensure([], () => r(require('@/components/card/tab/blog.vue')), 'blog')
const tre = r => require.ensure([], () => r(require('@/components/card/tab/tre.vue')), 'tre')
const art = r => require.ensure([], () => r(require('@/components/card/tab/art.vue')), 'art')
const product = r => require.ensure([], () => r(require('@/components/card/tab/product.vue')), 'product')
const personCard = r => require.ensure([], () => r(require('@/components/card/personCard.vue')), 'personCard')
const companyCard = r => require.ensure([], () => r(require('@/components/card/companyCard.vue')), 'companyCard')
const artcle = r => require.ensure([], () => r(require('@/components/artcle/index.vue')), 'artcle')
const wxlogin = r => require.ensure([], () => r(require('@/components/login/wxlogin')), 'wxlogin')
const moreMember = r => require.ensure([], () => r(require('@/components/card/moreMember.vue')), 'moreMember')
const myCompany = r => require.ensure([], () => r(require('@/components/mine/index/myCompany')), 'myCompany')
const editCard = r => require.ensure([], () => r(require('@/components/mine/index/editCard')), 'editCard')
const receiveCard = r => require.ensure([], () => r(require('@/components/mine/index/receiveCard')), 'receiveCard')
const myCollect = r => require.ensure([], () => r(require('@/components/mine/index/myCollect')), 'myCollect')
const myExchange = r => require.ensure([], () => r(require('@/components/mine/index/myExchange')), 'myExchange')
const myRecommend = r => require.ensure([], () => r(require('@/components/mine/index/myRecommend')), 'myRecommend')
const myArtical = r => require.ensure([], () => r(require('@/components/mine/index/myArtical')), 'myArtical')
const myGood = r => require.ensure([], () => r(require('@/components/mine/index/myGood')), 'myGood')
const erweima = r => require.ensure([], () => r(require('@/components/mine/index/erweima')), 'erweima')
const inviteMe = r => require.ensure([], () => r(require('@/components/mine/index/inviteMe')), 'inviteMe')
const inviteMeList = r => require.ensure([], () => r(require('@/components/mine/index/inviteMe/inviteMeList')), 'inviteMeList')
const inviteList = r => require.ensure([], () => r(require('@/components/mine/index/inviteMe/inviteList')), 'inviteList')
const myTalk = r => require.ensure([], () => r(require('@/components/mine/index/myTalk')), 'myTalk')
const searchPosition = r => require.ensure([], () => r(require('@/components/mine/index/searchPosition')), 'searchPosition')
const position = r => require.ensure([], () => r(require('@/components/mine/index/position')), 'position')
const tips = r => require.ensure([], () => r(require('@/components/mine/index/tips')), 'tips')
const industry = r => require.ensure([], () => r(require('@/components/mine/index/industry')), 'industry')
const collectAll = r => require.ensure([], () => r(require('@/components/mine/index/myCollect/collectAll')), 'collectAll')
const collectTerm = r => require.ensure([], () => r(require('@/components/mine/index/myCollect/collectTerm')), 'collectTerm')
const exchange = r => require.ensure([], () => r(require('@/components/mine/index/myExchange/exchange')), 'exchange')
const beExchange = r => require.ensure([], () => r(require('@/components/mine/index/myExchange/beExchange')), 'beExchange')
const recommendMe = r => require.ensure([], () => r(require('@/components/mine/index/myRecommend/recommendMe')), 'recommendMe')
const recommend = r => require.ensure([], () => r(require('@/components/mine/index/myRecommend/recommend')), 'recommend')
const editDetail = r => require.ensure([], () => r(require('@/components/mine/index/editDetail')), 'editDetail')
const follow = r => require.ensure([], () => r(require('@/components/mine/index/follow')), 'follow')
const followMe = r => require.ensure([], () => r(require('@/components/mine/index/followMe')), 'followMe')
const commonCard = r => require.ensure([], () => r(require('@/components/mine/index/commonCard')), 'commonCard')
const toRecList = r => require.ensure([], () => r(require('@/components/mine/index/toRecList')), 'toRecList')
const apply = r => require.ensure([], () => r(require('@/components/mine/index/apply')), 'toRecList')
const applyList = r => require.ensure([], () => r(require('@/components/mine/index/myApply/applyList')), 'applyList')
const applyMeList = r => require.ensure([], () => r(require('@/components/mine/index/myApply/applyMeList')), 'applyMeList')




Vue.use(Router)

const myrouter=new Router({
//mode:'history',
  routes: [
      {
        path:'',
        redirect:'/search',
      },
      {
        path:'/moreMember',
        name:'moreMember',
        component:moreMember,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/wxlogin',
        name:'wxlogin',
        component:wxlogin,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //首页
      {
        path:'/search',
        name:'search',
        component: resolve => require(['@/components/search/index.vue'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //列表页
      {
        path:'/search/searchList',
        name:'searchList',
        component: resolve => require(['@/components/search/searchList'], resolve),
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      //个人名片
      {
        path:'/card/personCard',
        name:'personCard',
        component: personCard,
        children:[{
          path:'/personCard/art',
          name:'art',
          component: art,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/personCard/tre',
          name:'tre',
          component: tre,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/personCard/product',
          name:'product',
          component: product,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/personCard/blog',
          name:'blog',
          component: blog,
          meta: {
            keepAlive: false,
            isActive:false
          }
        }],
        meta: {
          keepAlive: true,
          isActive:false
        },
        redirect:'/personCard/tre',
      },
      //公司名片
      {
        path:'/card/companyCard',
        name:'companyCard',
        component: companyCard,
        children:[{
          path:'/companyCard/art',
          component: art,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/companyCard/tre',
          component: tre,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/companyCard/product',
          name:'productc',
          component: product,
          meta: {
            keepAlive: false,
            isActive:false
          }
        },{
          path:'/companyCard/blog',
          name:'blogc',
          component: blog,
          meta: {
            keepAlive: false,
            isActive:false
          }
        }],
        meta: {
          keepAlive: true,
          isActive:false
        },
        redirect:'/companyCard/tre',
      },
      //名帖文章
      {
        path:'/card/article',
        name:'cardArticle',
        component: resolve => require(['@/components/card/cardArticle'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //@一下
      {
        path:'/reply',
        name:'reply',
        component: reply,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //名帖产品
      {
        path:'/card/product',
        name:'cardProduct',
        component: resolve => require(['@/components/card/cardProduct'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //名帖音乐
      {
        path:'/card/addMusic',
        name:'cardMusic',
        component: resolve => require(['@/components/card/addMusic'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //名帖添加动态
      {
        path:'/card/addTrend',
        name:'cardAddTrend',
        component: resolve => require(['@/components/card/addTrend'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //名帖添加文章
      {
        path:'/card/addArticle',
        name:'cardAddArticle',
        component: resolve => require(['@/components/card/addArticle'], resolve),
        meta: {
          keepAlive: true ,// 需要缓存
          isActive:false
        }
      },
      //名帖添加产品
      {
        path:'/card/addProduce',
        name:'cardAddProducee',
        component: resolve => require(['@/components/card/addProduce'], resolve),
        meta: {
          keepAlive: true, // 需要缓存
          isActive:false
        }
      },
       //名帖邀请下属
      {
        path:'/card/addChildren',
        name:'cardAddChildren',
        component: resolve => require(['@/components/card/addChildren'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/artcle',
        name:'artcle',
        component:artcle,
        meta: {
          keepAlive: true, // 需要缓存
          isActive:false
        }
      },
      //动态
      {
        path:'/trends',
        name:'trends',
        component: resolve => require(['@/components/trends/index'], resolve),
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
          path:'/trends/add',
          name:'trendsAdd',
          component: resolve => require(['@/components/trends/addTrend'], resolve),
          meta: {
            keepAlive: true, // 需要缓存
            isActive:false
          }
      },
      // 我的首页
      {
        path:'/mine',
        name:'mine',
        component:mineIndex,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //名帖二维码
      {
        path:'/mine/erweima',
        name:'erweima',
        component:erweima,
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      //添加名帖
      {
        path:'/mine/addCard',
        name:'addCard',
        component:addCard,
        meta: {
          keepAlive: false, // 需要缓存
          isActive:false
        }
      },
      //地址搜索
      {
        path:'/mine/searchPosition',
        name:'searchPosition',
        component:searchPosition,
        meta: {
          keepAlive: true // 需要缓存
        }
      },
       //地址选择
      {
        path:'/mine/position',
        name:'position',
        component:position,
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      //行业选择
      {
        path:'/mine/industry',
        name:'industry',
        component:industry,
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      //标签选择
      {
        path:'/mine/tips',
        name:'tips',
        component:tips,
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      {
        path:'/mine/myCard',
        name:'myCard',
        component:myCard,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/mine/commonCard',
        name:'commonCard',
        component:commonCard,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/mine/myCompany',
        name:'myCompany',
        component:myCompany,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //编辑列表
      {
        path:'/mine/editCard',
        name:'editCard',
        component:editCard,
        meta: {
          keepAlive: false // 需要缓存
        }
      },//编辑详情
      {
        path:'/mine/editDetail',
        name:'editDetail',
        component:editDetail,
        meta: {
          keepAlive: true, // 需要缓存
          isActive:false
        }
      },
      {
        path:'/mine/receiveCard',
        name:'receiveCard',
        component:receiveCard,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //我的搜藏
      {
        path:'/mine/myCollect',
        name:'myCollect',
        component:myCollect,
        children:[
          {
            path:'/mine/myCollect/collectAll',
            name:'collectAll',
            component:collectAll,
            meta: {
              keepAlive: false // 需要缓存
            }
          },{
            path:'/mine/myCollect/collectTerm',
            name:'collectTerm',
            component:collectTerm,
            meta: {
              keepAlive: false // 需要缓存
            }
          }
        ],
        redirect:'/mine/myCollect/collectAll',
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/mine/myExchange',
        name:'myExchange',
        component:myExchange,
        children:[
          {
            path:'/mine/myExchange/exchange',
            name:'exchange',
            component:exchange,
            meta: {
              keepAlive: false // 需要缓存
            }
          },{
            path:'/mine/myExchange/beExchange',
            name:'beExchange',
            component:beExchange,
            meta: {
              keepAlive: false // 需要缓存
            }
          }
        ],
        redirect:'/mine/myExchange/beExchange',
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/mine/myRecommend',
        name:'myRecommend',
        component:myRecommend,
        children:[
          {
            path:'/mine/myRecommend/recommend',
            name:'recommend',
            component:recommend,
            meta: {
              keepAlive: false // 需要缓存
            }
          },{
            path:'/mine/myRecommend/recommendMe',
            name:'recommendMe',
            component:recommendMe,
            meta: {
              keepAlive: false // 需要缓存
            }
          }
        ],
        redirect:'/mine/myRecommend/recommend',
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      //我的关注
      {
        path:'/mine/toRecList',
        name:'toRecList',
        component:toRecList,
        meta: {
          keepAlive: false // 不需要缓存
        }
      },
      //我的关注
      {
        path:'/mine/follow',
        name:'follow',
        component:follow,
        meta: {
          keepAlive: false // 不需要缓存
        }
      },
      //我的粉丝
      {
        path:'/mine/followMe',
        name:'followMe',
        component:followMe,
        meta: {
          keepAlive: false // 不需要缓存
        }
      },
      {
        path:'/mine/myArtical',
        name:'myArtical',
        component:myArtical,
        meta: {
          keepAlive: false // 不需要缓存
        }
      },
      {
        path:'/mine/myGood',
        name:'myGood',
        component:myGood,
        meta: {
          keepAlive: false // 需要缓存
        }
      },
      {
        path:'/mine/inviteMe',
        name:'inviteMe',
        component:inviteMe,
        children:[
          {
            path:'/mine/inviteMe/inviteList',
            name:'inviteList',
            component:inviteList,
            meta: {
              keepAlive: false // 不需要缓存
            }
          },{
            path:'/mine/inviteMe/inviteMeList',
            name:'inviteMeList',
            component:inviteMeList,
            meta: {
              keepAlive: false // 不需要缓存
            }
          }
        ],
        redirect:'/mine/inviteMe/inviteMeList',
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      {
        path:'/mine/apply',
        name:'apply',
        component:apply,
        children:[
          {
            path:'/mine/apply/applyList',
            name:'applyList',
            component:applyList,
            meta: {
              keepAlive: false // 不需要缓存
            }
          },{
            path:'/mine/apply/applyMeList',
            name:'applyMeList',
            component:applyMeList,
            meta: {
              keepAlive: false // 不需要缓存
            }
          }
        ],
        redirect:'/mine/apply/applyList',
        meta: {
          keepAlive: true // 需要缓存
        }
      },
      {
        path:'/mine/myTalk',
        name:'myTalk',
        component:myTalk,
        meta: {
          keepAlive: false // 需要缓存
        }
      }
  ]
})

myrouter.beforeEach((to, from, next) => {
  // alert('fuck')
  if(window.location.href.indexOf('wxlogin')==-1){
      if(window.location.href.indexOf('html?')!=-1){
        sessionStorage.setItem('curl',decodeURIComponent(window.location.href.split('?')[1]))
      }else{
        sessionStorage.setItem('curl',window.location.hash.slice(1))
      }
  }
  var sid=sessionStorage.getItem('sid')?sessionStorage.getItem('sid'):getCookie('sid');
  if(!sid&&to.name!='wxlogin'){
      hasuid()
      next(false)
  }else if(sid && !sessionStorage.getItem('sid')){
      sessionStorage.setItem('sid',sid)
      sessionStorage.setItem('uid',getCookie('uid')); 
      var allpath = decodeURIComponent(window.location.href)
      if(!from.name && allpath.indexOf('html?')>-1){
          allpath = allpath.replace('html?','html#')
          allpath = allpath.substring(0, allpath.lastIndexOf('#/'))
          window.location.href = allpath
          next(false)
      }else{
        next()
      }
  }else if(sid && !getCookie('sid')){
      setCookie('sid',sid,1*1000);
      setCookie('uid',sessionStorage.uid,1*1000);
      var allpath = decodeURIComponent(window.location.href)
      if(!from.name && allpath.indexOf('html?')>-1){
          allpath = allpath.replace('html?','html#')
          allpath = allpath.substring(0, allpath.lastIndexOf('#/'))
          window.location.href = allpath
          next(false)
      }else{
        next()
      }
  }else{
    next()
  }
  
})
myrouter.afterEach((to, from) => {
    if(to.name== 'cardArticle' || to.name=='tre'||to.name=='art' || to.name=='product' || to.path == '/companyCard/tre' || to.path == '/companyCard/art' || to.path == '/companyCard/product'){
        setTimeout(function(){
             wexinsdk()
        },1000)
    }
})
export default myrouter
